//
//  JUFNHomeTableViewController.swift
//  WiBoPM
//
//  Created by 陈俊峰 on 2017/1/4.
//  Copyright © 2017年 陈俊峰. All rights reserved.
/*
 1.刷新首页界面,请求数据,拿到用户信息,并将其显示到界面.
 
 */

import UIKit
import YYModel
class JUFNHomeTableViewController: JUFNBaseTableViewController {
    
    var vc:UIViewController?
    
    //    var userInfoStatuses : [JUFNUserStatus]?
    let JUFNRetweetedCellID = "JUFNRetweeted"
    let JUFNStatusTableViewCellID = "JUFNStatusTableViewCellID"
    
    lazy var homeViewModel = JUFNHomeTBViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        loadData()
    }
   @objc private func loadData() {
        
        self.homeViewModel.requestUserInfo(isPullUP: (indicator?.isAnimating)!) { (isSuccess) in
        
            if isSuccess {
                //刷新UI要单独抽出来,不要和网络请求数据放在一起
                self.tableView.reloadData()
                self.indicator?.stopAnimating()
                self.refresh?.currentStatus = .Normal
            }else{
                print("请求出错误")
            }
        }
        
    }
    lazy var indicator : UIActivityIndicatorView? = {
        let activityView = UIActivityIndicatorView(activityIndicatorStyle: .gray)
        return activityView
        
    }()
    private func setupUI() {
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", title: nil, target: self, action: #selector(clickRightBarButtonItem))
        self.loginRegisterView.updateVisitor(imageName: nil, isMain: true, text: nil)
        //对转发微博的cell进行注册
        let retweetedCellNib = UINib(nibName: "JUFNRetweeted", bundle: nil)
        
        let origionlNib = UINib(nibName: "JUFNStatusTableViewCell", bundle: nil)
        if JUFNAuthorViewModel.shared.uesrLogin {
            self.tableView.register(retweetedCellNib, forCellReuseIdentifier: JUFNRetweetedCellID)
            self.tableView.register(origionlNib, forCellReuseIdentifier: JUFNStatusTableViewCellID)
            self.tableView.separatorStyle = .none
            
            //表尾视图
            self.tableView.tableFooterView = indicator
            
//            self.refreshControl = UIRefreshControl()
//            
//            refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
            //使用自定义的refreshControl控件,继承自UIControl,不能继承自UIRefreshControl,因为继承自UIRefreshControl,会默认生成小菊花
            view.addSubview(refresh!)
            refresh?.addTarget(self, action: #selector(loadData), for: .valueChanged)
            
        }
        
    }
    
    @objc private func clickRightBarButtonItem() {
        let vc = JUFNSaoYiSaoViewController()
        
        _ = self.navigationController?.pushViewController(vc, animated: true)
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.homeViewModel.homeCellModelS.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let homeCellModel = self.homeViewModel.homeCellModelS[indexPath.row]
        let cellID = (homeCellModel.userModel?.retweeted_status == nil) ? JUFNStatusTableViewCellID : JUFNRetweetedCellID
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! JUFNStatusTableViewCell
        cell.cellModel = homeCellModel
        return cell
    }
    
    //设置约束,不适用自定义行高的方式来计算行高,由于tableView.dequeque这个方法中会自动调用tableview,所以这里再调用tableView.dequequeReusableCell方法会死循环,递归
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        //1.使用重用标识符来创建Cell,这里的Cell是不显示的,只是用来计算行高
        let homeCellModel = self.homeViewModel.homeCellModelS[indexPath.row]
        let cellID = (homeCellModel.userModel?.retweeted_status == nil) ? JUFNStatusTableViewCellID : JUFNRetweetedCellID
        let nibName = (cellID == JUFNStatusTableViewCellID) ? "JUFNStatusTableViewCell" : "JUFNRetweeted"
        //2.依靠这里的Cell来计算行高
        let sizeCell = UINib.init(nibName: nibName, bundle: nil).instantiate(withOwner: nil, options: nil).last! as! JUFNStatusTableViewCell
        sizeCell.cellModel = homeCellModel
        sizeCell.layoutIfNeeded()
        print(sizeCell.toolBar.frame.maxY)
        return sizeCell.toolBar.frame.maxY
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row == self.homeViewModel.homeCellModelS.count - 2 {
            indicator?.startAnimating()
            loadData()
        }
    }
    lazy var refresh : JUFNRefreashControl? = JUFNRefreashControl()
    
}


